package jsyf.oa.service.apply;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.dao.apply.MoneyCopierDao;
import jsyf.oa.entity.apply.MoneyCopierEntity;
import jsyf.oa.entity.apply.MoneyEntity;
import jsyf.oa.facade.apply.MoneyCopierFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018/1/18 0018.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class MoneyCopierService implements MoneyCopierFacade {
    @Autowired
    private MoneyCopierDao moneycopierDao;

    @Override
    public String save(String jsonData) {
        MoneyEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), MoneyEntity.class);
        String mainId = entity.getMain().getId();
        moneycopierDao.removeByCondition(" AND FK_MONEY_ID = ? ", new String[]{mainId});
        //添加
        List<MoneyCopierEntity> moneyCopierEntityList = entity.getCopiers();
        for (int i = 0; i < moneyCopierEntityList.size(); i++) {
            moneyCopierEntityList.get(i).setFkMoneyId(mainId);
            this.moneycopierDao.add(moneyCopierEntityList.get(i));
        }
        return entity.getMain().getId();
    }

    @Override
    public List<JSONObject> getByMoneyId(String moneyId) {
        return moneycopierDao.getJSONListByCondition(" AND FK_MONEY_ID = ?", new Object[]{moneyId});
    }
}


